package www.xpu.edu.app;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import www.xpu.edu.app.job.Activity_job_2;

import static android.content.Context.MODE_PRIVATE;


/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment1 extends Fragment {
    private View mView;

    /*@Override
    public void onDestroy() {
        super.onDestroy();
        ((ViewGroup)mView.getParent()).removeAllViews();
    }*/



    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        // return inflater.inflate(R.layout.fragment_fragment1, container, false);

        if (mView == null) {
            mView = inflater.inflate(R.layout.fragment_fragment1, container, false);
        }
        //sendRequest();
        Spinner spinner1=mView.findViewById(R.id.spinner1);
        Spinner spinner2=mView.findViewById(R.id.spinner2);

        final String[] str = new String[]{"请选择学院", "纺织与材料学院", "机电工程学院", "电子信息学院", "环境与化学工程学院","管理学院", "服装与艺术设计学院", "人文社会科学学院", "理学院", "计算机科学学院","艺术工程学院"};
        final String[] str2 = new String[]{"请选择时间", "最近", "1小时内", "今日", "明日"};
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this.getActivity(), R.layout.support_simple_spinner_dropdown_item, str);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this.getActivity(), R.layout.support_simple_spinner_dropdown_item, str2);
        spinner1.setAdapter(adapter1);
        spinner2.setAdapter(adapter2);
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position!=0)
                Toast.makeText(getActivity(),"您点击了"+str[position]+"查找功能正在开发中",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position!=0)
                    Toast.makeText(getActivity(),"您点击了"+str2[position]+"查找功能正在开发中",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        /*ListView listView=mView.findViewById(R.id.talk_list);

        TalkAdapter adapter = new TalkAdapter(getActivity(), R.layout.talk_list_layout, talkList);
        listView.setAdapter(adapter);*/
        sendRequest();
        List<Talk> talkList = new ArrayList<>();
        SharedPreferences pref=Objects.requireNonNull(getContext()).getSharedPreferences("data",MODE_PRIVATE);
        String talk=pref.getString("talk","");
        System.out.println(pref.getString("talk",""));
     /*List<JavaBean> list =JSON.parseObject(talk, new TypeReference<List<JavaBean>>() {
        });*/
      Gson gson=new Gson();
       List<JavaBean> list=new ArrayList<>();
       list=gson.fromJson(talk,new TypeToken<List<JavaBean>>(){}.getType());
      if(list==null)
      {
         /* Intent intent=new Intent(getActivity(),getActivity());
          startActivity(new Intent(getActivity(),getActivity()));*/
         startActivity(new Intent(getActivity(),getActivity().getClass()));
          System.out.println("跳转到本身,达到刷新数据的目的");
      }
        final List<JavaBean> list2=list;
        System.out.println(list != null ? list.size() : 0);
        System.out.println(list != null ? list.toString() : null);
       final String[] add={"大学生活动中心","西纺报告厅","1号教学楼","2号教学楼","3号教学楼","4号教学楼","5号教学楼","6号教学楼"};
        int k = new Random().nextInt(add.length - 1);
        final int k2=k;
        //显示最新的十条
        if((list != null ? list.size() : 0) >10) {
            for (int i = list.size()-1; i > list.size()-11; i--) {
                talkList.add(new Talk(R.drawable.logo2, list.get(i).getName(),/*list.get(i).getAdd()*/add[new Random().nextInt(add.length - 1)], list.get(i).getTime(), list.get(i).getContext()));
            }
        }
        else {
            for (int i = (list != null ? list.size() : 0) - 1; i >= 0; i--) {
                talkList.add(new Talk(R.drawable.logo_lantian, list != null ? list.get(i).getName() : null, "西安工程大学-" + (i + 1) + "号楼", list.get(i).getTime(), list.get(i).getContext()));
            }
        }
        TalkAdapter adapter = new TalkAdapter(getActivity(), R.layout.talk_list_layout, talkList);
        ListView listView=mView.findViewById(R.id.talk_list);
        listView.setAdapter(adapter);

        final List<JavaBean> finalList = list;
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String test = finalList != null ? finalList.get((list2 != null ? list2.size() : 0) - position - 1).getContext() : null;
                //list.size()-position-1
                String str2="时间"+finalList.get(position).getTime()+"地点"+add[new Random().nextInt(add.length - 1)];
                String str1=finalList.get(position).getName();
                Intent intent=new Intent(getActivity(),Activity_test.class);
                intent.putExtra("data1",str1);
                intent.putExtra("data2",str2);
                intent.putExtra("data",test);
                startActivity(intent);
                //startActivity(new Intent(getActivity(),Activity_job_2.class));
            }
        });



      //  sendRequest();




       /* for (int i = 0; i < 20; i++) {
            talkList.add(new Talk(R.drawable.logo_lantian,"蓝天教育","西安工程大学-"+(i+1)+"号楼","明天14:30-16:30","null"));
        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               String test= talkList.get(position).getAdd().toString()+talkList.get(position).getName().toString()+talkList.get(position).getTime()+String.valueOf(talkList.get(position).getImageID());
                Toast.makeText(getActivity(),"您点击了"+test,Toast.LENGTH_LONG).show();
            }
        });*/
       // sendRequest();
        return mView;
    }



    private void sendRequest() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            //.url("http://192.168.123.196:8080/test20190329/select0403")
                            .url("http://188.131.250.4:8080/xpu/selecttalk")
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    SharedPreferences.Editor editor=getContext().getSharedPreferences("data",MODE_PRIVATE).edit();
                    editor.putString("talk",responseData);
                    editor.apply();
                    /*List<JavaBean> list =JSON.parseObject(responseData, new TypeReference<List<JavaBean>>() {
                    });

                    System.out.println(list);
                    System.out.println(list.size());
                    for(JavaBean bean:list)
                    {
                        System.out.println(bean.getId()+""+bean.getName()+bean.getTime());
                    }
*/


                    //final List<JavaBean> finalList = list;
                    /*listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            String test = finalList.get(position).getContext();
                            Intent intent=new Intent(getActivity(),Activity_test.class);
                            intent.putExtra("data",test);
                            startActivity(intent);
                            //startActivity(new Intent(getActivity(),Activity_job_2.class));
                        }
                    });*/
                   // Toast.makeText(getContext(),list.toString(),Toast.LENGTH_LONG).show();
                    //frag2.setText(list.toString());
                  //  frag2.setText(list.size()+list.get(0).getContext());
                    //测试成功
                    //frag2.setText(list.get(0).getContext());
                   // frag2.setText(list.size());
                    /*List<Talk> talkList = new ArrayList<>();
                    //显示最新的十条
                   if(list.size()>10) {
                        for (int i = list.size()-1; i > list.size()-11; i--) {
                            talkList.add(new Talk(R.drawable.logo_lantian, list.get(i).getName(), "西安工程大学-" + (i + 1) + "号楼", list.get(i).getTime(), list.get(i).getContext()));
                        }
                    }
                    else {
                        for (int i = list.size() - 1; i >= 0; i--) {
                            talkList.add(new Talk(R.drawable.logo_lantian, list.get(i).getName(), "西安工程大学-" + (i + 1) + "号楼", list.get(i).getTime(), list.get(i).getContext()));
                        }
                    }
                    TalkAdapter adapter = new TalkAdapter(getActivity(), R.layout.talk_list_layout, talkList);
                    listView.setAdapter(adapter);

                    final List<JavaBean> finalList = list;
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            String test = finalList.get(position).getContext();

                            Intent intent=new Intent(getActivity(),Activity_test.class);
                            intent.putExtra("data",test);
                            startActivity(intent);
                            //startActivity(new Intent(getActivity(),Activity_job_2.class));
                        }
                    });*/
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }




}
